package com.ztgf.commodity.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CategoryShieldBrandRelationMapper;
import com.ztgf.commodity.models.bo.CategoryShieldBrandRelationBO;
import com.ztgf.commodity.models.bo.CategoryShieldBrandRelationSaveBO;
import com.ztgf.commodity.models.dto.CategoryShieldBrandRelationDTO;
import com.ztgf.commodity.models.dto.CommodityCategoryDTO;
import com.ztgf.commodity.models.service.ICategoryShieldBrandRelationService;
import com.ztgf.commodity.models.service.ICommodityCategoryService;
import com.ztgf.commodity.models.vo.CategoryShieldBrandRelationVO;
import com.ztgf.commodity.models.vo.ShieldBrandParentVO;
import com.ztgf.commodity.models.vo.ShieldBrandVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分类盾构品牌关系表控制器
 *
 * @author chenyu.wang
 * @Date 2020-06-02 15:11:41
 */
@DubboService
public class CategoryShieldBrandRelationServiceImpl extends ServiceImpl<CategoryShieldBrandRelationMapper, CategoryShieldBrandRelationDTO> implements ICategoryShieldBrandRelationService {

    @Resource
    private YmlConfig ymlConfig;
    @DubboReference
    private ICommodityCategoryService categoryService;

    @Override
    public boolean add(CategoryShieldBrandRelationSaveBO saveBO) {
        //根据分类id查询类目,最顶级类目才能添加关联
        CommodityCategoryDTO categoryDTO = categoryService.selectById(saveBO.getCategoryId());
        if (categoryDTO != null) {
            if (categoryDTO.getParentId() == 0) {
                CategoryShieldBrandRelationDTO dto = new CategoryShieldBrandRelationDTO();
                BeanUtils.copyProperties(saveBO, dto);
                Long id = ymlConfig.getPkId();
                dto.setId(id);
                return this.insert(dto);
            } else {
                return false;
            }
        }
        return false;
    }

    @Override
    public Page<CategoryShieldBrandRelationVO> queryShieldBrandRelationshipList(CategoryShieldBrandRelationBO bo) {
        Page<CategoryShieldBrandRelationVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<CategoryShieldBrandRelationVO> list = baseMapper.queryShieldBrandRelationshipList(page, bo);
        page.setRecords(list);
        return page;
    }

    @Override
    public ShieldBrandParentVO queryShieldBrands(Long id) {
        ShieldBrandParentVO vo = new ShieldBrandParentVO();
        List<ShieldBrandVO> list = baseMapper.queryShieldBrands(id);
        vo.setName("适用盾构品牌");
        vo.setList(list);
        return vo;
    }

    @Override
    public boolean deleteByCateoryId(Long categoryId) {
        Wrapper<CategoryShieldBrandRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_id", categoryId);
        List<CategoryShieldBrandRelationDTO> list = this.selectList(wrapper);
        if (list != null && list.size() > 0) {
            return this.deleteBatchIds(list);
        }
        return false;
    }
}
